from django.db import models
from db.base_models import BaseModel
from django.contrib.auth.models import AbstractUser
# Create your models here.


class UserStatus(BaseModel):
    """
    用户状态
    """
    user_status = models.CharField(max_length=10, verbose_name="用户状态")

    class Meta:
        db_table = "user_status"
        verbose_name = "用户状态"
        verbose_name_plural = verbose_name


class Role(BaseModel):
    """
    用户角色
    """
    role_name = models.CharField(max_length=20, verbose_name="角色名称")

    class Meta:
        db_table = "role"
        verbose_name = "用户角色"
        verbose_name_plural = verbose_name


class Area(BaseModel):
    """
    省市级联
    """
    area_name = models.CharField(max_length=20, verbose_name="地名")
    pid = models.ForeignKey("self", on_delete=models.DO_NOTHING, blank=True, null=True)

    class Meta:
        db_table = "areainfo"
        verbose_name = "地区表"
        verbose_name_plural = verbose_name


class Sex(BaseModel):
    """
    性别
    """
    sex_name = models.CharField(max_length=8, verbose_name="性别")

    class Meta:
        db_table = "sex"
        verbose_name = "性别"
        verbose_name_plural = verbose_name


class User(AbstractUser, BaseModel):
    """
    继承django自带的User模型
    """
    phone = models.CharField(max_length=11, verbose_name="手机号", blank=True, null=True)

    class Meta:
        db_table = "users"
        verbose_name = "用户表"
        verbose_name_plural = verbose_name


class UserInfo(BaseModel):
    """
    用户的详细信息
    """
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="关联用户表")
    sex = models.ForeignKey(Sex, blank=True, null=True, on_delete=models.CASCADE, verbose_name="关联性别")
    age = models.CharField(max_length=4, blank=True, null=True, verbose_name="年龄")
    icon = models.ImageField(upload_to='icons', blank=True, null=True, verbose_name="头像")
    identity = models.CharField(max_length=18, blank=True, null=True, verbose_name="身份证号")
    birthday = models.CharField(max_length=20, blank=True, null=True, verbose_name="生日")
    zt_name = models.ForeignKey(UserStatus, on_delete=models.CASCADE, verbose_name="用户状态", blank=True, null=True)

    class Meta:
        db_table = "userinfo"
        verbose_name = "个人信息"
        verbose_name_plural = verbose_name


class Address(BaseModel):
    """
    用户地址
    """
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="关联用户表")
    province = models.ForeignKey(Area, on_delete=models.CASCADE, related_name="province",
                                 blank=True, null=True, verbose_name="省")
    town = models.ForeignKey(Area, on_delete=models.CASCADE, related_name="town",
                             blank=True, null=True, verbose_name="市")
    district = models.ForeignKey(Area, on_delete=models.CASCADE, related_name="district",
                                 blank=True, null=True, verbose_name="县")
    place = models.CharField(max_length=100, blank=True, null=True, verbose_name="详细地址")
    zt_name = models.ForeignKey(UserStatus, on_delete=models.CASCADE, verbose_name="地址状态")

    class Meta:
        db_table = "address"
        verbose_name = "用户地址"
        verbose_name_plural = verbose_name








